#include"struct.h"
#include"main.h"
int judge1(int studentcode) {  //判断函数
	int  x = 1;
	for (int i = 0; i < stucnt; i++) {
		if (studentcode == stus[i].stucode)
			x = 0;
	}
	return x;
}
int judge2(int selectcode) { //判断函数
	int  x = 1;
 
	for (int i = 0; i < coursecnt; i++) {
		if (selectcode == courses[i].coursecode)
			x = 0;
	}
	return x;
}
int judge3(int selecttime) {   //判断函数
	int  x = 1;
	for (int i = 0; i < coursecnt; i++) {
		if (selecttime == courses[i].totalhour)
			x = 0;
	}
	return x;
}
int judge4(int selectctime) {  //判断函数
	int  x = 1;
	for (int i = 0; i < coursecnt; i++) {
		if (selectctime == courses[i].classhour)
			x = 0;
	}
	return x;
}
int judge5(int selectetime) {   //判断函数
	int  x = 1;
	for (int i = 0; i < coursecnt; i++) {
		if (selectetime == courses[i].exercisehour)
			x = 0;
	}
	return x;
}
int judge6(int selectcredit) {   //判断函数
	int  x = 1;
	for (int i = 0; i < coursecnt; i++) {
		if (selectcredit == courses[i].credit)
			x = 0;
	}
	return x;
}
int judge7(int selectterm) {   //判断函数
	int x = 1;
	for (int i = 0; i < coursecnt; i++) {
		if (selectterm == courses[i].term)
			x = 0;
	}
	return x;
}
void selectcode2(int k) {   //通过课程编号选课
	int t = 0, s;
	printf("请问想要选择几门课程?\n");
	int selectcode, count;
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入你要选择课程的编号!\n");
			scanf("%d", &selectcode);
			if (judge2(selectcode)) {
				printf("输入课程编号错误，请重新输入!\n");
			} else {
				for (int m = 0; m < coursecnt; m++) {
					if (selectcode == courses[m].coursecode)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			}
			if (count > 1)
				t++;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void selectname2(int k) {    //通过课程名称选课
	int t = 0, s;
	printf("请问想要选择几门课程?\n");
	int  count, a = 2;
	char selectname[20];
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入您要选择课程的名称!\n");
			scanf("%s", selectname);
			for (int i = 0; i < coursecnt; i++) {
				if (strcmp(selectname, courses[i].coursename) == 0)
					a = 1;
			}
			if (a == 1) {
				for (int m = 0; m < coursecnt; m++) {
					if (strcmp(selectname, courses[m].coursename) == 0)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			} else if (a == 2) {
				printf("输入课程名称错误，请重新输入!\n");
			}
			if (count > 1)
				t++;
			a = 2;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void selecttype2(int k) {    //通过课程性质选课
	int t = 0, s, a = 2;
	printf("请问想要选择几门课程?\n");
	char selecttype[20];
	int  count;
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入你要选择课程的课程性质!\n");
			scanf("%s", selecttype);
			for (int i = 0; i < coursecnt; i++) {
				if (strcmp(selecttype, courses[i].coursetype) == 0)
					a = 1;
			}
			if (a == 1) {
				for (int m = 0; m < coursecnt; m++) {
					if (strcmp(selecttype, courses[m].coursetype) == 0)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			} else if (a == 2) {
				printf("输入课程性质错误，请重新输入!\n");
			}
			if (count > 1)
				t++;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void selecttime2(int k) {   //通过课程总学时选课
	int t = 0, s;
	printf("请问想要选择几门课程?\n");
	int selecttime, count;
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入你要选择课程的总学时!\n");
			scanf("%d", &selecttime);
			if (judge3(selecttime)) {
				printf("输入课程总学时错误，请重新输入!\n");
			} else {
				for (int m = 0; m < coursecnt; m++) {
					if (selecttime == courses[m].totalhour)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			}
			if (count > 1)
				t++;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void selectctime2(int k) {   //通过课程授课学时选课
	int t = 0, s;
	printf("请问想要选择几门课程?\n");
	int selectctime, count;
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入你要选择课程的授课学时!\n");
			scanf("%d", &selectctime);
			if (judge4(selectctime)) {
				printf("输入课程授课学时错误，请重新输入!\n");
			} else {
				for (int m = 0; m < coursecnt; m++) {
					if (selectctime == courses[m].classhour)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			}
			if (count > 1)
				t++;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void selectetime2(int k) {   //通过课程上机或实验学时选课
	int t = 0, s;
	printf("请问想要选择几门课程?\n");
	int selectetime, count;
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入你要选择课程的上机学时!\n");
			scanf("%d", &selectetime);
			if (judge5(selectetime)) {
				printf("输入课程上机学时错误，请重新输入!\n");
			} else {
				for (int m = 0; m < coursecnt; m++) {
					if (selectetime == courses[m].coursecode)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			}
			if (count > 1)
				t++;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void selectcredit2(int k) {   //通过课程学分选课
	int t = 0, s;
	printf("请问想要选择几门课程?\n");
	int selectcredit, count;
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入你要选择课程的学分!\n");
			scanf("%d", &selectcredit);
			if (judge6(selectcredit)) {
				printf("输入课程学分错误，请重新输入!\n");
			} else {
				for (int m = 0; m < coursecnt; m++) {
					if (selectcredit == courses[m].credit)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			}
			if (count > 1)
				t++;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void selectterm2(int k) {  //通过课程开课学期选课
	int t = 0, s;
	printf("请问想要选择几门课程?\n");
	int selectterm, count;
	scanf("%d", &count);
	if (count < 1)
		printf("选课失败,请按照要求进行选课!\n");
	else if (count >= 1 && count <= coursecnt)
		for (int i = 0; i < count; i++) {
			printf("请输入你要选择课程的开课学期!\n");
			scanf("%d", &selectterm);
			if (judge7(selectterm)) {
				printf("输入课程开课学期错误，请重新输入!\n");
			} else {
				for (int m = 0; m < coursecnt; m++) {
					if (selectterm == courses[m].term)
						s = m;
				}
				stus[k].selectcode[t] = courses[s].coursecode;
				stus[k].selectnumber++;
				printf("选课成功!\n");
			}
			if (count > 1)
				t++;
		} else
		printf("选课失败，请按要求进行选课!\n");
}
void Courseinsert() {    //课程信息录入
	int i;
	int cnt = 0;
	printf("请输入录入课程数量：\n");
	scanf("%d", &cnt);
	coursecnt = cnt;
	printf("请输入课程信息（格式如下：1 C语言 编程语言 30 20 10 1 1)：\n");
	printf("课程编号 课程名称 课程性质 总学时 授课学时 上机学时 学分 开课学期\n");
	for (i = 0; i < cnt; i++) {
		scanf("%d", &courses[i].coursecode);
		scanf("%s", courses[i].coursename);
		scanf("%s", courses[i].coursetype);
		scanf("%d", &courses[i].totalhour);
		scanf("%d", &courses[i].classhour);
		scanf("%d", &courses[i].exercisehour);
		scanf("%f", &courses[i].credit);
		scanf("%d", &courses[i].term);
	}
	printf("数据已录入完毕\n");
}
void Coursebrowse() {  //课程信息浏览
	int cnt, i;
	printf("请输入想浏览多少门课程：\n");
	scanf("%d", &cnt);
	if (cnt < 1 || cnt > coursecnt) {
		printf("输入数据错误，请重新输入！\n");
		Coursebrowse();
	} else {
		for (i = 0; i < cnt; i++) {
			printf("%d %s %s %d %d %d %f %d\n",
			       courses[i].coursecode,
			       courses[i].coursename,
			       courses[i].coursetype,
			       courses[i].totalhour,
			       courses[i].classhour,
			       courses[i].exercisehour,
			       courses[i].credit,
			       courses[i].term);
		}
		printf("课程数据已经全部展示!\n");
	}
}
void Coursequery() {  //查询课程信息
	printf("8.根据课程性质查询\n");
	printf("9.根据学分查询\n");
	printf("请输入所要查询编号\n");
	int choice;
	scanf("%d", &choice);
	switch (choice) {
		case 8:
			char coursetype[20];
			printf("请输入课程性质\n");
			scanf("%s", coursetype);
			for (int i = 0; i < coursecnt; i++) {
				if (strcmp(coursetype, courses[i].coursetype) == 0) {
					printf("%d %s %s %d %d %d %f %d\n",
					       courses[i].coursecode,
					       courses[i].coursename,
					       courses[i].coursetype,
					       courses[i].totalhour,
					       courses[i].classhour,
					       courses[i].exercisehour,
					       courses[i].credit,
					       courses[i].term);
				}
			}
			printf("数据已全部展示\n");
			break;
		case 9:
			float credit;
			printf("请输入所要查询的课程学分\n");
			scanf("%f", &credit);
			for (int i = 0; i < coursecnt; i++) {
				if (credit == courses[i].credit) {
					printf("%d %s %s %d %d %d %f %d\n",
					       courses[i].coursecode,
					       courses[i].coursename,
					       courses[i].coursetype,
					       courses[i].totalhour,
					       courses[i].classhour,
					       courses[i].exercisehour,
					       courses[i].credit,
					       courses[i].term);
				}
 
			}
			printf("数据已经全部展示\n");
			break;
		default:
			printf("输入选项错误，请重新输入！\n");
			Coursequery();
	}
}
void Studentinsert() {   //学生信息录入
	int cnt;
	printf("请输入录入几个学生的信息\n");
	scanf("%d", &cnt);
	stucnt = cnt;
	printf("请输入学生信息(请如以下格式:2207820235 张三\n");
	printf("学号 学生姓名\n");
	for (int i = 0; i < cnt; i++) {
		scanf("%d", &stus[i].stucode);
		scanf("%s", stus[i].stuname);
	}
	printf("学生信息已经录入完毕\n");
}
void Selectcode(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selectcode2(k);
		}
	}
}
void Selectname(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selectname2(k);
		}
	}
}
void Selectype(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selecttype2(k);
		}
	}
}
 
void Selecttime(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selecttime2(k);
		}
	}
}
void Selectctime(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selectctime2(k);
		}
	}
}
void Selectetime(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selectetime2(k);
		}
	}
}
void Selectcredit(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selectcredit2(k);
		}
	}
}
void Selectterm(int studentcode) {
	for (int k = 0; k < stucnt; k++) {
		if (studentcode == stus[k].stucode) {
			selectterm2(k);
		}
	}
}
 
void query(int studentcode) {   //查询已选课程信息
	for (int i = 0; i < stucnt; i++) {
		if (studentcode == stus[i].stucode) {
			printf("姓名:%s\n", stus[i].stuname);
			printf("学号:%d\n", stus[i].stucode);
			printf("选择课程的编号:");
			for (int j = 0; j < coursecnt; j++) {
				printf("%3d", stus[i].selectcode[j]);
			}
			printf("\n");
		}
	}
}
void Select1() {      //展示可选课程信息
	for (int i = 0; i < coursecnt; i++) {
		printf("%d %s %s %d %d %d %f %d\n",
		       courses[i].coursecode,
		       courses[i].coursename,
		       courses[i].coursetype,
		       courses[i].totalhour,
		       courses[i].classhour,
		       courses[i].exercisehour,
		       courses[i].credit,
		       courses[i].term);
	}
	printf("课程数据已全部展示!\n");
}
void Select2() {  //学生选课
	int studentcode;
	printf("请输入学生学号!\n");
	scanf("%d", &studentcode);
	if (judge1(studentcode)) {
		printf("不存在此学生信息，请先录入学生信息!\n");
	} else {
		int choice;
		printf("13.根据课程编号选择\n");
		printf("14.根据课程名称选择\n");
		printf("15.根据课程性质选择\n");
		printf("16.根据课程总学时选择\n");
		printf("17.根据课程授课学时选择\n");
		printf("18.根据课程上机学时选择\n");
		printf("19.根据课程学分选择\n");
		printf("20.根据课程开设学期选择\n");
		printf("请输入操作编号！\n");
		scanf("%d", &choice);
		switch (choice) {
			case 13 :
				Selectcode(studentcode);
				break;
			case 14:
				Selectname(studentcode);
				break;
			case 15:
				Selectype(studentcode);
				break;
			case 16:
				Selecttime(studentcode);
				break;
			case 17:
				Selectctime(studentcode);
				break;
			case 18:
				Selectetime(studentcode);
				break;
			case 19:
				Selectcredit(studentcode);
				break;
			case 20:
				Selectterm(studentcode);
				break;
		}
	}
}
void Select3() {
	int studentcode;
	printf("请输入想要查询学生的学号!\n");
	scanf("%d", &studentcode);
	if (judge1(studentcode))
		printf("无此学生信息，请先录入信息!\n");
	else {
		query(studentcode);
	}
}
void Studentcourse() {   //学生选课
	int choice;
	printf("10.查询可选课程\n");
	printf("11.选择课程\n");
	printf("12.查询已选课程\n");
	printf("请输入你的操作编号\n");
	scanf("%d", &choice);
	switch (choice) {
		case 10: {
			Select1();
			break;
		}
		case 11: {
			Select2();
			break;
		}
		case 12: {
			Select3();
			break;
		}
		default : {
			printf("输入操作编号错误!\n");
		}
	}
}
void Statistic1() {    //统计学生选课数量
	printf("请输入想要查询学生的学号!\n");
	int stucode;
	scanf("%d", &stucode);
	for (int i = 0; i < stucnt; i++) {
		if (stucode == stus[i].stucode) {
			printf("该学生一共选择的课程数量:");
			printf("%d\n", stus[i].selectnumber);
		}
	}
}
int Statistic2(int coursecode) {
	int acount = 0;
	for (int i = 0; i < stucnt; i++) {
		for (int k = 0; k < stucnt; k++) {
			if (coursecode == stus[i].selectcode[k])
				acount++;
		}
	}
	return acount;
}
void Statistic3() {    //统计选择某门课程的人数
	int coursecode, count = 0;
	printf("请输入你想要查询课程的编号!\n");
	scanf("%d", &coursecode);
	count = Statistic2(coursecode);
	for (int i = 0; i < coursecnt; i++) {
		if (coursecode == courses[i].coursecode) {
			printf("选择该课程的人数:");
			printf("%d\n", count);
		}
	}
}
void Statistic4() {
 
}
void Statistic() {  //统计
	printf("21.统计学生选了多少门课程\n");
	printf("22.统计课程的选择人数\n");
	printf("23.统计选择某门课程的学生信息\n");
	int choice;
	printf("请输入你的选择!\n");
	scanf("%d", &choice);
	switch (choice) {
		case 21:
			Statistic1();
			break;
		case 22:
			Statistic3();
			break;
		case 23:
			Statistic4();
			break;
	}
}
